package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysNoticeDo;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通知公告Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    private final SysNoticeMapper sysNoticeMapper;

    /**
     * 查询通知公告
     */
    @Override
    public SysNoticeDo queryById(Long noticeId) {
        return sysNoticeMapper.selectById(noticeId);
    }

    /**
     * 查询通知公告列表
     */
    @Override
    public TableDataInfo<SysNoticeDo> queryPageList(SysNoticeDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysNoticeDo> lqw = buildQueryWrapper(entity);
        Page<SysNoticeDo> result = sysNoticeMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询通知公告列表
     */
    @Override
    public List<SysNoticeDo> queryList(SysNoticeDo entity) {
        LambdaQueryWrapper<SysNoticeDo> lqw = buildQueryWrapper(entity);
        return sysNoticeMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysNoticeDo> buildQueryWrapper(SysNoticeDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysNoticeDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getNoticeTitle()), SysNoticeDo::getNoticeTitle, entity.getNoticeTitle());
        lqw.eq(StringUtils.isNotBlank(entity.getNoticeType()), SysNoticeDo::getNoticeType, entity.getNoticeType());
        lqw.eq(StringUtils.isNotBlank(entity.getNoticeContent()), SysNoticeDo::getNoticeContent, entity.getNoticeContent());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysNoticeDo::getStatus, entity.getStatus());
        return lqw;
    }

    /**
     * 新增通知公告
     */
    @Override
    public Boolean insert(SysNoticeDo entity) {
        SysNoticeDo add = BeanUtil.toBean(entity, SysNoticeDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysNoticeMapper.insert(add) > 0;
        if (flag) {
            entity.setNoticeId(add.getNoticeId());
        }
        return flag;
    }

    /**
     * 修改通知公告
     */
    @Override
    public Boolean updateById(SysNoticeDo entity) {
        SysNoticeDo update = BeanUtil.toBean(entity, SysNoticeDo.class);
        validEntityBeforeSave(update);
        return sysNoticeMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysNoticeDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除通知公告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysNoticeMapper.deleteBatchIds(ids) > 0;
    }
}
